﻿// -------------------------------------------------------------------------
//    @FileName         :    MockItemDesc.cpp
//    @Author           :    gaoyi
//    @Date             :    2025/1/20
//    @Email            :    445267987@qq.com
//    @Module           :    MockItemDesc
//
// -------------------------------------------------------------------------

#include "MockItemDesc.h"
#include <iostream>

// MockItemDesc实现
MockItemDesc::MockItemDesc() : m_initialized(false) {
}

MockItemDesc::~MockItemDesc() {
    Clear();
}

int MockItemDesc::Initialize() {
    if (m_initialized) {
        return 0;
    }
    
    // 清空现有数据
    Clear();
    
    // 创建默认测试物品
    CreateDefaultTestItems();
    
    m_initialized = true;
    return 0;
}

const E_Item* MockItemDesc::GetDesc(int itemId) const {
    auto it = m_itemDescs.find(itemId);
    if (it != m_itemDescs.end()) {
        return it->second.get();
    }
    return nullptr;
}

void MockItemDesc::AddItemDesc(int itemId, const E_Item& desc) {
    m_itemDescs[itemId] = std::make_unique<E_Item>(desc);
}

void MockItemDesc::Clear() {
    m_itemDescs.clear();
}

size_t MockItemDesc::GetDescCount() const {
    return m_itemDescs.size();
}

void MockItemDesc::CreateDefaultTestItems() {
    // 创建普通物品
    AddItemDesc(TestItemIds::NORMAL_ITEM_1, 
                CreateItemDesc(TestItemIds::NORMAL_ITEM_1, "普通物品1", TestItemTypes::NORMAL, 
                              TestQuality::WHITE, 1, false));
    
    AddItemDesc(TestItemIds::NORMAL_ITEM_2, 
                CreateItemDesc(TestItemIds::NORMAL_ITEM_2, "普通物品2", TestItemTypes::NORMAL, 
                              TestQuality::GREEN, 1, true));
    
    AddItemDesc(TestItemIds::NORMAL_ITEM_3, 
                CreateItemDesc(TestItemIds::NORMAL_ITEM_3, "普通物品3", TestItemTypes::NORMAL, 
                              TestQuality::BLUE, 1, false));
    
    // 创建装备
    AddItemDesc(TestItemIds::WEAPON_SWORD, 
                CreateEquipDesc(TestItemIds::WEAPON_SWORD, "铁剑", TestEquipTypes::WEAPON, 
                               TestQuality::WHITE, 1));
    
    AddItemDesc(TestItemIds::WEAPON_BOW, 
                CreateEquipDesc(TestItemIds::WEAPON_BOW, "精制弓", TestEquipTypes::WEAPON, 
                               TestQuality::GREEN, 5));
    
    AddItemDesc(TestItemIds::ARMOR_HELMET, 
                CreateEquipDesc(TestItemIds::ARMOR_HELMET, "铁盔", TestEquipTypes::HELMET, 
                               TestQuality::WHITE, 1));
    
    AddItemDesc(TestItemIds::ARMOR_CHEST, 
                CreateEquipDesc(TestItemIds::ARMOR_CHEST, "皮甲", TestEquipTypes::CHEST, 
                               TestQuality::WHITE, 1));
    
    AddItemDesc(TestItemIds::ARMOR_LEGS, 
                CreateEquipDesc(TestItemIds::ARMOR_LEGS, "铁腿甲", TestEquipTypes::LEGS, 
                               TestQuality::GREEN, 3));
    
    // 创建消耗品
    AddItemDesc(TestItemIds::POTION_HP, 
                CreateConsumableDesc(TestItemIds::POTION_HP, "生命药水", 1, 50));
    
    AddItemDesc(TestItemIds::POTION_MP, 
                CreateConsumableDesc(TestItemIds::POTION_MP, "魔法药水", 2, 50));
    
    AddItemDesc(TestItemIds::FOOD_BREAD, 
                CreateConsumableDesc(TestItemIds::FOOD_BREAD, "面包", 3, 100));
    
    // 创建材料
    AddItemDesc(TestItemIds::MATERIAL_WOOD, 
                CreateItemDesc(TestItemIds::MATERIAL_WOOD, "木材", TestItemTypes::MATERIAL, 
                              TestQuality::WHITE, 200, false));
    
    AddItemDesc(TestItemIds::MATERIAL_STONE, 
                CreateItemDesc(TestItemIds::MATERIAL_STONE, "石材", TestItemTypes::MATERIAL, 
                              TestQuality::WHITE, 200, false));
    
    AddItemDesc(TestItemIds::MATERIAL_IRON, 
                CreateItemDesc(TestItemIds::MATERIAL_IRON, "铁矿", TestItemTypes::MATERIAL, 
                              TestQuality::GREEN, 100, false));
    
    // 创建可堆叠物品
    AddItemDesc(TestItemIds::STACKABLE_ARROW, 
                CreateItemDesc(TestItemIds::STACKABLE_ARROW, "箭矢", TestItemTypes::NORMAL, 
                              TestQuality::WHITE, 1000, false));
    
    AddItemDesc(TestItemIds::STACKABLE_COIN, 
                CreateItemDesc(TestItemIds::STACKABLE_COIN, "金币", TestItemTypes::NORMAL, 
                              TestQuality::WHITE, 10000, false));
    
    // 创建唯一物品
    AddItemDesc(TestItemIds::UNIQUE_QUEST_ITEM, 
                CreateItemDesc(TestItemIds::UNIQUE_QUEST_ITEM, "任务物品", TestItemTypes::NORMAL, 
                              TestQuality::BLUE, 1, true));
    
    AddItemDesc(TestItemIds::UNIQUE_KEY, 
                CreateItemDesc(TestItemIds::UNIQUE_KEY, "神秘钥匙", TestItemTypes::NORMAL, 
                              TestQuality::PURPLE, 1, true));
    
    // 创建特殊物品
    AddItemDesc(TestItemIds::TREASURE_MAP, 
                CreateItemDesc(TestItemIds::TREASURE_MAP, "藏宝图", TestItemTypes::TREASURE_MAP, 
                              TestQuality::ORANGE, 1, true));
    
    AddItemDesc(TestItemIds::PARTNER_EQUIP, 
                CreateItemDesc(TestItemIds::PARTNER_EQUIP, "伙伴装备", TestItemTypes::PARTNER_EQUIP, 
                              TestQuality::BLUE, 1, false));
    
    AddItemDesc(TestItemIds::FASHION_ITEM, 
                CreateItemDesc(TestItemIds::FASHION_ITEM, "时装", TestItemTypes::FASHION, 
                              TestQuality::PURPLE, 1, true));
}

E_Item MockItemDesc::CreateItemDesc(int itemId, const std::string& name, int itemType, 
                                   int quality, int maxStack, bool canBind) {
    E_Item item;
    item.set_id(itemId);
    item.set_name(name);
    item.set_item_type(itemType);
    item.set_quality(quality);
    item.set_max_stack(maxStack);
    item.set_can_bind(canBind);
    item.set_price(100);  // 默认价格
    item.set_level_limit(1);  // 默认等级限制
    return item;
}

E_Item MockItemDesc::CreateEquipDesc(int equipId, const std::string& name, int equipType,
                                    int quality, int level) {
    E_Item equip = CreateItemDesc(equipId, name, TestItemTypes::EQUIP, quality, 1, false);
    equip.set_equip_type(equipType);
    equip.set_level_limit(level);
    equip.set_price(500);  // 装备价格更高
    return equip;
}

E_Item MockItemDesc::CreateConsumableDesc(int itemId, const std::string& name, 
                                         int effectType, int maxStack) {
    E_Item consumable = CreateItemDesc(itemId, name, TestItemTypes::CONSUMABLE, 
                                      TestQuality::WHITE, maxStack, false);
    consumable.set_use_effect(effectType);
    consumable.set_price(50);  // 消耗品价格较低
    return consumable;
}

// MockItemDescManager实现
MockItemDescManager* MockItemDescManager::s_instance = nullptr;

MockItemDescManager::MockItemDescManager() : m_mockDesc(nullptr) {
}

MockItemDescManager::~MockItemDescManager() {
    Cleanup();
}

MockItemDescManager* MockItemDescManager::Instance() {
    if (!s_instance) {
        s_instance = new MockItemDescManager();
    }
    return s_instance;
}

const E_Item* MockItemDescManager::GetItemDesc(int itemId) const {
    if (m_mockDesc) {
        return m_mockDesc->GetDesc(itemId);
    }
    return nullptr;
}

void MockItemDescManager::SetMockItemDesc(MockItemDesc* mockDesc) {
    m_mockDesc = mockDesc;
}

void MockItemDescManager::Cleanup() {
    if (s_instance) {
        delete s_instance;
        s_instance = nullptr;
    }
    m_mockDesc = nullptr;
} 